package music;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;



/**
 * Created by liujian on 16/2/17.
 */
public class ScreenOffBroadcast {

    private Context context;
    private boolean isRegister = false;

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            receiveBroadcast(intent);
        }
    };

    public ScreenOffBroadcast(Context context) {
        this.context = context.getApplicationContext();
    }

    /**
     * 注册广播
     */
    public void registerBroadcast() {
        if (null != context && null != mReceiver && !isRegister) {
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
            intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
            context.registerReceiver(mReceiver, intentFilter);
            isRegister = true;
        }
    }

    /**
     * 释放广播
     */
    public void unregisterBroadcast() {
        if (null != context && null != mReceiver && isRegister) {
            context.unregisterReceiver(mReceiver);
            isRegister = false;
        }
    }

    private void receiveBroadcast(Intent intent) {
        String action = intent.getAction();
        if (action.equals(Intent.ACTION_SCREEN_OFF)) {
//            if (Params.player.mediaPlayer.isPlaying()) {
//                Intent lockScreen = new Intent(context, LockScreenActivity.class);
//                lockScreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                context.startActivity(lockScreen);
//            }
        }
    }

}
